package com.longge;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-03-17 01:07
 */
public class MinCameraCover {

    public int minCameraCover(TreeNode root) {
        int[] res = dfs(root);
        return Math.min(res[0],res[1]);
    }

    private int[] dfs(TreeNode root) {
        if (root == null) return new int[3];
        int[] l = dfs(root.left);
        int[] r = dfs(root.right);
        int[] res = new int[3];
        res[0] = Math.min(l[0], Math.min(l[1], l[2])) + Math.min(r[0], Math.min(r[1], r[2])) + 1;
        res[1] = Math.min(l[0] + Math.min(r[0], r[1]), r[0] + Math.min(l[0], l[1]));
        res[2] = l[1] + r[1];
        return res;
    }
}
